<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Projectmodel extends CI_Model {

	protected $_CI;

	
	public static $searchField = array(
		'project_status' => 'project_status',
	);
	public static $likeField = array(
		'project_name' => 'project_name',
		'project_code' => 'project_code',
	);

	public function __construct(){
		parent::__construct();
		$this->_CI = & get_instance();
	}
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
	public function getProjectListSection($assist){
		$this->db->from('project');
		if(!empty($assist['where'])) $this->db->where($assist['where']);
		if(!empty($assist['like'])) $this->db->like($assist['like']);
		$count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

		$this->db->order_by('project_id','desc');
		if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
		$query = $this->db->get();
		$list = $query->result_array();

		return array($list,$count);
	}

	public function getProjectCss($project_id){
		$this->db->from('project_css');
		$this->db->where('project_id',$project_id);
		$this->db->order_by('project_css_id','desc');
		$this->db->limit(1);
		$query = $this->db->get();
		$item = $query->row_array();

		return $item;
	}

	public function getProjectJs($project_id){
		$this->db->from('project_js');
		$this->db->where('project_id',$project_id);
		$this->db->order_by('project_js_id','desc');
		$this->db->limit(1);
		$query = $this->db->get();
		$item = $query->row_array();

		return $item;
	}


	public function getProject($project_id){
		$this->db->from('project');
		$this->db->where('project_id',$project_id);
		$this->db->limit(1);
		$query = $this->db->get();
		$item = $query->row_array();

		return $item;
	}

	public function getProjectByCode($project_code){
		$this->db->from('project');
		$this->db->where('project_code',$project_code);
		$this->db->where('project_status',STATUS_ACTIVE);
		$this->db->limit(1);
		$query = $this->db->get();
		$item = $query->row_array();

		return $item;
	}

	public function getProjectFile($project_file_id){
		$this->db->from('project_file');
		$this->db->where('project_file_id',$project_file_id);
		$this->db->limit(1);
		$query = $this->db->get();
		$item = $query->row_array();

		return $item;
	}

	public function getProjectSpriteList($project_id){
		$this->db->from('sprite');
		$this->db->where('project_id',$project_id);
		$this->db->where('sprite_status !=',STATUS_DELETE);
		$this->db->order_by('sprite_x','asc');
		$query = $this->db->get();
		$list = $query->result_array();

		return $list;
	}

	public function getSprite($sprite_id){
		$this->db->from('sprite');
		$this->db->where('sprite_id',$sprite_id);
		$this->db->limit(1);
		$query = $this->db->get();
		$item = $query->row_array();

		return $item;
	}

	public function getActiveProjectIdList(){
		$this->db->select('project_id');
		$this->db->from('project');
		$this->db->where('project_status',STATUS_ACTIVE);
		$query = $this->db->get();
		$list = $query->result_array();

		$list = extractColumn($list,'project_id');
		$list[] = -1;
		return $list;
	}
/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
	public function createProject($info){
		$this->db->insert('project',$info);
		return $this->db->insert_id();
	}

	public function createProjectCss($info){
		$this->db->insert('project_css',$info);
		return $this->db->insert_id();
	}

	public function createProjectJs($info){
		$this->db->insert('project_js',$info);
		return $this->db->insert_id();
	}

	public function createProjectFileBatch($info){
		if(!empty($info)){
			$this->db->insert_batch('project_file',$info);
		}
	}

	public function createProjectSprite($info){
		$this->db->insert('sprite',$info);
		return $this->db->insert_id();
	}

	public function createProjectSpriteBatch($info){
		if(!empty($info)){
			$this->db->insert_batch('sprite',$info);
		}
	}
/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
	public function editProject($project_id,$info){
		$this->db->where('project_id',$project_id);
		$this->db->update('project',$info);
	}

	public function editProjectFile($project_file_id,$info){
		$this->db->where('project_file_id',$project_file_id);
		$this->db->update('project_file',$info);
	}

	public function editProjectSpriteBatch($info){
		$this->db->update_batch('sprite',$info,'sprite_id');
		return true;
	}
/*
| -------------------------------------------------------------------
|  DELETE Functions
| -------------------------------------------------------------------
*/
	public function removeProjectFile($project_file_id){
		$this->db->where('project_file_id',$project_file_id);
		$this->db->delete('project_file');
		return true;
	}
/*
| -------------------------------------------------------------------
|  CHECK Functions
| -------------------------------------------------------------------
*/
	public function checkNameAvailable($name,$project_id = 0){
		$this->db->from('project');
		$this->db->where('project_id !=',$project_id);
		$this->db->where('project_name',$name);
		$count = $this->db->count_all_results();

		return ($count == 0);
	}

	public function checkCodeAvailable($code,$project_id = 0){
		$this->db->from('project');
		$this->db->where('project_id !=',$project_id);
		$this->db->where('project_code',$code);
		$count = $this->db->count_all_results();

		return ($count == 0);
	}

	public function checkFileNameAvailable($name,$project_id = 0){
		$this->db->from('project_file');
		$this->db->where('project_id',$project_id);
		$this->db->where('project_file_name',$name);
		$count = $this->db->count_all_results();

		return ($count == 0);
	}

/*
| -------------------------------------------------------------------
|  GENERATE Functions
| -------------------------------------------------------------------
*/
	public function generateSprite($project,$width,$height){
		$sprite_list = $this->getProjectSpriteList($project['project_id']);
		foreach($sprite_list as $key => &$record){
			$record['sprite_path'] = UPLOAD_PATH.$record['sprite_path'];
			if($record['sprite_status'] != STATUS_ACTIVE || !file_exists($record['sprite_path'])) {
				unset($sprite_list[$key]);
				continue;	
			}
			$info = getimagesize($record['sprite_path']);
			$record['sprite_width'] = $info[0];
			$record['sprite_height'] = $info[1];
			$right = $record['sprite_width'] + $record['sprite_x'];
			$down = $record['sprite_height'] + $record['sprite_y'];
		}
        $canvas = new Imagick();
        $canvas->newImage($width,$height,new ImagickPixel('transparent'));

        foreach($sprite_list as $sprite){
            $src = new Imagick($sprite['sprite_path']);
            $canvas->compositeImage($src,imagick::COMPOSITE_DEFAULT,$sprite['sprite_x'],$sprite['sprite_y']);
            $src->clear();
            $src->destroy();
		}
        $canvas->setImageFormat('png');
        $canvas->writeImage(UPLOAD_PATH.$project['project_code'].'/sprite.png');
        $canvas->clear();
        $canvas->destroy();
	}
/*
| -------------------------------------------------------------------
|  OUTPUT Functions
| -------------------------------------------------------------------
*/
	public function outputProjectList4Js(){
		$this->db->select('project_id,project_name');
		$this->db->from('project');
		$this->db->where('project_status',STATUS_ACTIVE);
		$this->db->order_by('project_id','desc');
		$query = $this->db->get();
		$list = $query->result_array();

		$result = array();
		foreach ($list as $record) {
			$result[] = array(
				$record['project_id'],
				$record['project_name'],
			);
		}

		return json_encode($result);
	}
}
